package com.yunzheng.studyLeaveSystem.modules.user.user_info.dao;

import com.yunzheng.studyLeaveSystem.modules.user.user_info.entity.User_info;
import com.yunzheng.studyLeaveSystem.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: YunZheng
 * @Date: 2023/01/30/10:52
 * @Description:
 */
public class User_infoDao {


    /**
     * 通过id查询用户信息
     *
     * @param id
     * @return
     */
    public User_info getUser_info(int id) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "select * from user_info where id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        User_info user_info = null;
        while (resultSet.next()) {
            String phone_num1 = resultSet.getString("phone_num");
            String password = resultSet.getString("password");
            String username = resultSet.getString("username");
            String gender = resultSet.getString("gender");
            String term = resultSet.getString("term");
            String direction = resultSet.getString("direction");
            String constellation = resultSet.getString("constellation");
            String hometown = resultSet.getString("hometown");
            String birthday = resultSet.getString("birthday");
            String qq = resultSet.getString("QQ");
            String wechat = resultSet.getString("wechat");
            String email = resultSet.getString("email");
            String signature = resultSet.getString("signature");
            user_info = new User_info(phone_num1, password, username, gender, term, direction,constellation, hometown, birthday, qq,wechat,email,signature,phone_num1);
        }
        DBUtils.release(resultSet, preparedStatement, connection);
        return user_info;
    }

    /**
     * 修改密码
     * @param password
     * @param id
     * @throws Exception
     */
    public void updatePassword(String password,int id) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "update user_info set password=? where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, password);
        preparedStatement.setInt(2, id);
        int rows = preparedStatement.executeUpdate();
        DBUtils.release(null,preparedStatement, connection);
    }

    /**
     * 查询密码
     * @param id
     * @return
     * @throws Exception
     */
    public String queryPassword(int id) throws Exception {
        String password = null;
        Connection connection = DBUtils.getCon();
        String sql = "select password from user_info where id = ?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            password = resultSet.getString("password");
        }
        DBUtils.release(resultSet,preparedStatement,connection);
        return password;
    }

    /**
     * 修改手机号(遗留)
     * @param phone_num
     * @throws Exception
     */
    public void updatePhone_num(String phone_num , String phone_num1) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "update user_info set phone_num=? where phone_num=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, phone_num);
        preparedStatement.setString(2, phone_num1);
        int rows = preparedStatement.executeUpdate();
        DBUtils.release(null,preparedStatement, connection);
    }


    /**
     * 修改个人信息页面
     * @param username
     * @param gender
     * @param term
     * @param direction
     * @param hometown
     * @param phone_num
     * @param birthday
     * @param QQ
     * @param email
     * @param signature
     * @return
     * @throws Exception
     */
    public void updateUser_info(String username,String gender, String term , String direction ,String constellation, String hometown, String phone_num , String birthday, String QQ , String wechat, String email ,String signature,int id) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "update user_info set username=?,gender=?,term=?,direction=?,constellation=?,hometown=?,phone_num=?,birthday=?,QQ=?,wechat=?,email=?,signature=? where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, gender);
        preparedStatement.setString(3, term);
        preparedStatement.setString(4, direction);
        preparedStatement.setString(5,constellation);
        preparedStatement.setString(6, hometown);
        preparedStatement.setString(7, phone_num);
        preparedStatement.setString(8, birthday);
        preparedStatement.setString(9, QQ);
        preparedStatement.setString(10, wechat);
        preparedStatement.setString(11, email);
        preparedStatement.setString(12, signature);
        preparedStatement.setInt(13, id);
        int rows = preparedStatement.executeUpdate();
        DBUtils.release(null,preparedStatement, connection);
    }

    /**
     * 头像旁边个人信息
     * @param id
     * @return
     * @throws Exception
     */
    public  String query(int id) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "select username,direction,term from user_info where id = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        String username = null;
        String direction = null;
        String term = null;
        while (resultSet.next()) {
             username = resultSet.getString("username");
             term = resultSet.getString("term");
             direction = resultSet.getString("direction");
        }
        DBUtils.release(resultSet, preparedStatement, connection);
        String Infor = direction + "-" + term + "-" + username;
        return  Infor;
    }


}
